package cn.caplike.demo.repository;

@SuppressWarnings("ALL")
public class ObjectAndClass {

    public static void main(String[] args) {

        // ~ X.isAssignableFrom(Y): Y 是否继承自 X, 即 X 是 Y 的超类或者接口

        System.out.println("Class.class.isAssignableFrom(Object.class): " + Class.class.isAssignableFrom(Object.class));
        System.out.println("Object.class.isAssignableFrom(Class.class): " + Object.class.isAssignableFrom(Class.class)); // Class.class 继承自 Object.class

        System.out.println("Animal.class.isAssignableFrom(Dog.class): " + Animal.class.isAssignableFrom(Dog.class)); // true

        // ~ instanceof
        annimalInstanceofDog();
    }

    private static void annimalInstanceofDog() {
        Dog dog = new Dog();
        Animal animal = dog;
        System.out.println("animal instanceof Dog: " + (animal instanceof Dog));
        System.out.println("Dog.class.isInstance(animal): " + (Dog.class.isInstance(animal)));
        System.out.println("Dog.class.isInstance(dog): " + (Dog.class.isInstance(dog)));
        System.out.println("Animal.class.isInstance(animal): " + (Animal.class.isInstance(animal))); // animal 是 Animal 的子类或者实例
        System.out.println("Animal.class.isInstance(dog): " + (Animal.class.isInstance(dog)));
    }

    public static class Animal {
        // ...
    }

    public static class Dog extends Animal {
        // ...
    }
}

